﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.HCLoad.Import;
using YArchitech.HCLoad.Parameters;
using YArchitech.Revit;

namespace YArchitech.HCLoad
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class HcAnnotate : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Result result;
			try
			{
				UIApplication application = commandData.Application;
				if (!SpaceSharedParameters.HasLoadParam(application.ActiveUIDocument.Document))
				{
					MessageBox.Show("还没有导入计算结果，请先导入负荷计算结果！");
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					new ImportForm(application, true).ShowDialog();
                    this.SetCateVisible(application.ActiveUIDocument.Document, (BuiltInCategory)(-2000300), true);
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		public void SetCateVisible(Document doc, BuiltInCategory builtInCate, bool bVisible)
		{
			Transaction transaction = new Transaction(doc, "www");
			transaction.Start();
			Category cate = doc.Settings.Categories.get_Item(builtInCate);
			doc.SetYJKVisibility(cate, bVisible);
			transaction.Commit();
		}
	}
}
